function [D2] = deformCosine(ZI,ZJ)
d = length(ZI);
side = sqrt(d);
image = reshape(ZI,side,side);
[x,y] = meshgrid(1:side,1:side);
for i = -2:2:2
    tx = (side-2*i-1)/(side-1)*(x(:)-1)+i+1;
    ty = y(:);
    ii = find(tx>=1 & tx<=side);
    tim = zeros(side,side);
    tx = tx(ii);
    ty = ty(ii);
    tim(ii) = interp2(x,y,image,tx,ty);
    TI = tim(:);
    [dist,~] = pdist2(TI,ZJ);
%     subplot(1,2,1);imagesc(image);
%     subplot(1,2,2);imagesc(tim);
%     drawnow;pause;
end
end